package cn.schtwz.baselib.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import cn.schtwz.baselib.R;

public class HeadView extends LinearLayout {
    private OnLeftIconClick onLeftIconClick;

    public HeadView(Context context) {
        this(context, null, -1);
    }

    public HeadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public HeadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HeadView);


        initView(context, ta);

    }

    private void initView(Context context, TypedArray ta) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.layout_head_view, this, true);
        Drawable left = ta.getDrawable(R.styleable.HeadView_left_icon);
        Drawable right = ta.getDrawable(R.styleable.HeadView_right_icon);
        String title = ta.getString(R.styleable.HeadView_title);
        ((TextView) rootView.findViewById(R.id.title_tv)).setText(title);
        if (null != left) {
            ((ImageView) rootView.findViewById(R.id.left_icon)).setImageDrawable(left);
            rootView.findViewById(R.id.left_icon).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onLeftIconClick) {
                        onLeftIconClick.setOnLeftIconOnclick();
                    }
                }
            });


        }
        if (null != right) {
            ((ImageView) rootView.findViewById(R.id.right_icon)).setImageDrawable(right);

        }

    }

    public void setOnLeftIconClick(OnLeftIconClick onLeftIconClick) {
        this.onLeftIconClick = onLeftIconClick;
    }

   public interface OnLeftIconClick {
        void setOnLeftIconOnclick();
    }


}
